package swingExtended.preferences;

import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import swingExtended.inputBrowsePanel.InputBrowserPanel;

/**
 *
 * @author g1wshimu
 * Created: Nov 15, 2013
 */
public class PreferenceGroup {

    private final Preferences PREF;
    private final List<BindableControl> CONTROLS = new ArrayList<>();

    public PreferenceGroup(Preferences pref) {
        this.PREF = pref;
    }

    public void add(final JTextField textbox, final String key, final String defaultVal) {
        CONTROLS.add(new BindableControl() {

            @Override
            public void initFromPreferences() {
                textbox.setText(PREF.get(key, defaultVal));
            }

            @Override
            public void savePreferences() {
                PREF.put(key, textbox.getText());
            }
        });
    }

    public void add(final JCheckBox checkbox, final String key, final boolean defaultSelected) {
        CONTROLS.add(new BindableControl() {

            @Override
            public void initFromPreferences() {
                checkbox.setSelected(PREF.getBoolean(key, defaultSelected));
            }

            @Override
            public void savePreferences() {
                PREF.putBoolean(key, checkbox.isSelected());
            }
        });
    }

    public void add(final JCheckBox checkbox, final String key) {
        add(checkbox, key, checkbox.isSelected());
    }

    public void add(final JComboBox dropDown, final String key, final int defaultIndex) {
        CONTROLS.add(new BindableControl() {

            @Override
            public void initFromPreferences() {
                dropDown.setSelectedIndex(PREF.getInt(key, defaultIndex));
            }

            @Override
            public void savePreferences() {
                PREF.putInt(key, dropDown.getSelectedIndex());
            }
        });
    }

    /*
    public void add(final JSpinner spinner, final String key, final Number defaultVal) {
        CONTROLS.add(new BindableControl() {

            @Override
            public void initFromPreferences() {
                
                spinner.setValue(PREF.get(key, defaultVal.toString()));
            }

            @Override
            public void savePreferences() {
                PREF.put(key, spinner.getValue().toString());
            }
        });
    }
    
    public void add(final JSpinner spinner, final String key) {
        CONTROLS.add(new BindableControl() {

            @Override
            public void initFromPreferences() {
                spinner.setValue(PREF.get(key, spinner.getValue().toString()));
            }

            @Override
            public void savePreferences() {
                PREF.put(key, spinner.getValue().toString());
            }
        });
    }*/
    
    public void add(final InputBrowserPanel panel, final String key, final String defaultVal) {
        CONTROLS.add(new BindableControl() {

            @Override
            public void initFromPreferences() {
                panel.setText(PREF.get(key, defaultVal));
            }

            @Override
            public void savePreferences() {
                PREF.put(key, panel.getText());
            }
        });
    }

    public void loadPreferences() {
        for (BindableControl control : CONTROLS) {
            control.initFromPreferences();
        }
    }

    public void savePreferences() {
        for (BindableControl control : CONTROLS) {
            control.savePreferences();
        }
    }

    private abstract class BindableControl {

        private BindableControl() {
        }

        public abstract void initFromPreferences();

        public abstract void savePreferences();
    }
}
